package com.toy.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.toy.entity.Cart;
import com.toy.entity.Orders;
import com.toy.entity.User;
import com.toy.entity.UserAddress;
import com.toy.exception.ToyMallException;
import com.toy.result.ResponseEnum;
import com.toy.service.CartService;
import com.toy.service.UserAddressService;
import com.toy.service.UserService;
import com.toy.vo.CartVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author admin
 * @since 2022-08-31
 */
@Controller
@RequestMapping("/cart")
@Slf4j
public class CartController {

    @Autowired
    private CartService cartService;
    @Autowired
    private UserAddressService userAddressService;

    /**
     *添加购物车
     * @return
     */
    @GetMapping("/add/{productId}/{price}/{quantity}")
    public String add(
            @PathVariable("productId") Integer productId,
            @PathVariable("price") Float price,
            @PathVariable("quantity") Integer quantity,
            HttpSession session){
        if (productId == null || price == null || quantity ==null){
            log.info("【添加购物车】参数为空");
            throw new ToyMallException(ResponseEnum.PARAMETER_NULL);
        }
        User user = (User) session.getAttribute("user");
        if (user == null){
            log.info("【添加购物车】当前为未登录状态");
            throw new ToyMallException(ResponseEnum.NOT_LOGIN);
        }else {
            //登录用户
            //购物车存在，加数量
            List<CartVO> cartVOList = this.cartService.findCartVOListByUserId(user.getId());
            for (CartVO cartVO : cartVOList) {
                if (cartVO.getProductId().equals(productId)){
                    Boolean update = cartService.update(cartVO.getId(),
                            cartVO.getQuantity() + quantity,
                            cartVO.getCost() + price * quantity);
                    if (!update){
                        log.info("【添加购物车】修改失败");
                        throw new ToyMallException(ResponseEnum.CART_ADD_ERROR);
                    }
                    return "redirect:/cart/get";
                }
            }
            //查询该用户的购物车记录
            Cart cart = new Cart();
            cart.setUserId(user.getId());
            cart.setProductId(productId);
            cart.setQuantity(quantity);
            cart.setCost(price * quantity);
            Boolean add = this.cartService.add(cart);
            if (!add){
                log.info("【添加购物车】添加失败");
                throw new ToyMallException(ResponseEnum.CART_ADD_ERROR);
            }
            return "redirect:/cart/get";
        }
    }

    /**
     * 查看购物车
     * @param session
     * @return
     */
    @GetMapping("/get")
    public ModelAndView get(HttpSession session){
        User user = (User) session.getAttribute("user");
        if (user == null){
            log.info("【查看购物车】当前为未登录状态");
            throw new ToyMallException(ResponseEnum.NOT_LOGIN);
        }
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("settlement1");
        modelAndView.addObject("cartList",
                this.cartService.findCartVOListByUserId(user.getId()));
        return modelAndView;
    }

    /**
     * 更新购物车
     * @return
     */
    @PostMapping("update/{id}/{quantity}/{cost}")
    @ResponseBody
    public String update(
            @PathVariable("id") Integer id,
            @PathVariable("quantity") Integer quantity,
            @PathVariable("cost") Float cost,
            HttpSession session){
        if (id == null || quantity == null || cost == null){
            log.info("【更新购物车】参数为空");
            throw new ToyMallException(ResponseEnum.PARAMETER_NULL);
        }
        User user = (User) session.getAttribute("user");
        if (user == null){
            log.info("【更新购物车】当前为未登录状态");
            throw new ToyMallException(ResponseEnum.NOT_LOGIN);
        }
        if (this.cartService.update(id, quantity, cost)){
            return "success";
        }
        return "fail";
    }

    /**
     * 删除购物车
     * @param id
     * @param session
     * @return
     */
    @GetMapping("/delete/{id}")
    public String delete(@PathVariable Integer id,HttpSession session){
        if (id == null){
            log.info("【删除购物车】参数为空");
            throw new ToyMallException(ResponseEnum.PARAMETER_NULL);
        }
        User user = (User) session.getAttribute("user");
        if (user == null){
            log.info("【删除购物车】当前为未登录状态");
            throw new ToyMallException(ResponseEnum.NOT_LOGIN);
        }
        Boolean delete = this.cartService.delete(id);
        if (delete){
            return "redirect:/cart/get";
        }
        return null;
    }

    /**
     * 确认结算
     * @param session
     * @return
     */
    @GetMapping("/confirm")
    public ModelAndView confirm(HttpSession session){
        //判断是否为登录用户
        User user = (User) session.getAttribute("user");
        if (user == null){
            log.info("【确认订单】当前为未登录状态");
            throw new ToyMallException(ResponseEnum.NOT_LOGIN);
        }
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("settlement2");
        modelAndView.addObject("cartList",
                this.cartService.findCartVOListByUserId(user.getId()));
        QueryWrapper<UserAddress> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", user.getId());
        modelAndView.addObject("addressList",
                this.userAddressService.list(queryWrapper));
        return modelAndView;
    }

    /**
     * 确认订单信息
     * @param userAddress
     * @param session
     * @return
     */
    @PostMapping("/commit")
    public ModelAndView commit(String userAddress,
                               String address,
                               String remark,
                               HttpSession session){
        if (userAddress == null  || address == null || remark == null){
            log.info("【提交订单】参数为空");
            throw new ToyMallException(ResponseEnum.PARAMETER_NULL);
        }
        User user = (User) session.getAttribute("user");
        if (user == null){
            log.info("【提交订单】当前为未登录状态");
            throw new ToyMallException(ResponseEnum.NOT_LOGIN);
        }
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("settlement3");
        Orders orders = this.cartService.commit(userAddress, address, remark, user);
        if (orders != null){
            modelAndView.addObject("orders", orders);
            modelAndView.addObject("cartList",
                    this.cartService.findCartVOListByUserId(user.getId()));
            return modelAndView;
        }
        return null;
    }
}

